// store/index.js
import { createStore } from "vuex";

export default createStore({
  state: {
    user: {
      username: "未登录",
      password: "",
    },
    isLogin: false,
    loginName: "未登录", //登录用户名
    in: 2, //已登录用户人数
    out: 0, //未登录用户人数
    loginNum: 0, //虚拟用户人数
    count: 0, //计数器
    good: {},
    myGoodsList: [],
    carList: [],
  },
  mutations: {
    setUser(state, user) {
      state.user = user;
    },
    removeUser(state) {
      state.user.username = "";
      state.user.password = "";
    },
    setIsLogin(state, isLogin) {
      state.isLogin = isLogin;
    },
    setGood(state, good) {
      state.good = good;
    },
    setMyGoodsList(state, myGoodsList) {
      state.myGoodsList = myGoodsList;
    },
    addMyGoodsList(state, myGoodsList) {
      state.myGoodsList.push(myGoodsList);
    },
    setCarList(state, carList) {
      state.carList = carList;
    },
    addCarList(state, carList) {
      state.carList.push(carList);
    },
    checkInCarList(state) {
      state.carList.splice(0, state.carList.length);
    },
	clearmyGoodsList(state) {
		console.log('clearmyGoodsList');
		state.myGoodsList.splice(0,state.myGoodsList.length);
		console.log(state.myGoodsList);
	},	
	//计数器count'增加
	add(state) {
		state.count++;
	},
	//计数器count减少
	minus(state) {
		state.count--;
	},
	//登录
	login(state,username) { //登录
		console.log('in...');
		
		state.isLogin = true;
		state.in++;
		state.loginName = username;
		// console.log(username);
		// console.log(state.loginName);
	},
	logout(state) { //退出登录
		state.isLogin = false;
		state.in--;
		state.count = 0;
		console.log('in...')
	},
	//增加虚拟用户数量,flag区分在线虚拟用户和离线虚拟用户
	//mutation的方法只能由两个参数，要传递多个参数都写在第二个参数里
	addLogNum(state, {
		loginNum,
		flag
	}) {
		if (flag)
			state.in += loginNum;
		else
			state.out += loginNum;
		state.loginNum = loginNum;
	}
  },
  actions: {
    asyncLogin({ commit }, user) {
      // 这里可以做异步操作，如请求服务器验证用户信息
      commit("setUser", user);
      console.log(user);
    },
    deleteUser({ commit }) {
      commit("removeUser");
    },
    setIsLogin({ commit }, isLogin) {
      commit("setIsLogin", isLogin);
    },
    setGood({ commit }, good) {
      commit("setGood", good);
    },
    setMyGoodsList({ commit }, myGoodsList) {
      commit("setMyGoodsList", myGoodsList);
    },
    addMyGoodsList({ commit }, myGoodsList) {
      commit("addMyGoodsList", myGoodsList);
    },
    setCarList({ commit }, carList) {
      commit("setCarList", carList);
    },
    addCarList({ commit }, carList) {
      commit("addCarList", carList);
    },
    checkInCarList({ commit }) {
      commit("checkInCarList");
    },
    //增加虚拟用户数量
	addlogNum({
		commit,
		state
	}, loginNum) {
		commit('addLogNum', loginNum);
	},
	//计数器减少
	minus(context) {
		context.commit("minus");
	},
	//5秒后退出登录
	tlogout({
		commit
	}) {
		console.log("wait 5s")
		setTimeout(() => {
			commit('logout');
		}, 5000)
	}
  },
  getters: {
    getUser: (state) => state.user,
    isLogin: (state) => state.isLogin,
    getGood: (state) => state.good,
    getMyGoodsList: (state) => state.myGoodsList,
    getCarList: (state) => state.carList,
    allNum: (state) => state.in + state.out,
	  loginNumofgetters: (state) => state.loginNum,
  },
});
